package com.dotstone.chipism.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.BrandSort;
import com.dotstone.chipism.bean.DeviceType;
import com.dotstone.chipism.util.BrandManager;
import com.dotstone.chipism.util.CharacterParser;
import com.dotstone.chipism.util.PinyinComparator;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;
import com.tiqiaa.client.IRemoteClient.CallbackOnBrandLoaded;
import com.tiqiaa.client.impl.RemoteClient;
import com.tiqiaa.remote.entity.Brand;
import com.tiqiaa.remote.entity.Page;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RemoteSelectionActivity extends BaseActivity {
	private LinearLayout mBackL;
	private TextView mStatusBar;
	private TextView mTitleTv;
	private LinearLayout mDiyL;
	private LinearLayout mAirL;
	private LinearLayout mFanL;
	private LinearLayout mTvL;
	private LinearLayout mSetL;
	private LinearLayout mProL;
	private LinearLayout mAudioL;
	private LinearLayout mBoxL;
	private LinearLayout mCameraL;
	private LinearLayout mDvdL;
	private LinearLayout mLightL;
	public static RemoteSelectionActivity remoteSelectionActivity;
	private RemoteClient rc;
	private List<Brand> bs = new ArrayList<Brand>();
	private CharacterParser characterParser;
	private PinyinComparator pinyinComparator;
	private List<BrandSort> brandSorts = new ArrayList<BrandSort>();
	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.click_device_diy:
			Intent intent = new Intent(getApplicationContext(), DIYActivity.class);
			startActivity(intent);
			break;
		case R.id.click_device_kongtiao:
			Intent intent1 = new Intent(getApplicationContext(), BrandSelectionActivity.class);
			intent1.putExtra("type", DeviceType.DEVICE_TYPE_AIR_CONTROLLER);
			startActivity(intent1);
			break;
		case R.id.click_device_fengshang:
			Intent intent2 = new Intent(getApplicationContext(), BrandSelectionActivity.class);
			intent2.putExtra("type", DeviceType.DEVICE_TYPE_FAN);
			startActivity(intent2);
			break;
		case R.id.click_device_tv:
			Intent intent3 = new Intent(getApplicationContext(), BrandSelectionActivity.class);
			intent3.putExtra("type", DeviceType.DEVICE_TYPE_TV);
			startActivity(intent3);
			break;
		case R.id.click_device_jidianhe:
			Intent intent4 = new Intent(getApplicationContext(), BrandSelectionActivity.class);
			intent4.putExtra("type", DeviceType.DEVICE_TYPE_SATELLITE);
			startActivity(intent4);
			break;
		case R.id.click_device_touying:
			Intent intent5 = new Intent(getApplicationContext(), BrandSelectionActivity.class);
			intent5.putExtra("type", DeviceType.DEVICE_TYPE_PROJECTOR);
			startActivity(intent5);
			break;
		case R.id.click_device_yinxiang:
			Intent intent7 = new Intent(getApplicationContext(), BrandSelectionActivity.class);
			intent7.putExtra("type", DeviceType.DEVICE_TYPE_AUDIO);
			startActivity(intent7);
			break;
		case R.id.click_device_box:
			// Intent intent8 = new
			// Intent(getApplicationContext(),RemoteBoxActivity.class);
			Intent intent8 = new Intent(getApplicationContext(), BrandSelectionActivity.class);
			intent8.putExtra("type", DeviceType.DEVICE_TYPE_BOX);
			startActivity(intent8);
			break;
		case R.id.click_device_cammare:
			// Intent intent9 = new
			// Intent(getApplicationContext(),RemoteCameraActivity.class);
			Intent intent9 = new Intent(getApplicationContext(), BrandSelectionActivity.class);
			intent9.putExtra("type", DeviceType.DEVICE_TYPE_CAMERA);
			startActivity(intent9);
			break;
		case R.id.click_device_dvd:
			// Intent intent11 = new
			// Intent(getApplicationContext(),RemoteDVDActivity.class);
			Intent intent11 = new Intent(getApplicationContext(), BrandSelectionActivity.class);
			intent11.putExtra("type", DeviceType.DEVICE_TYPE_DVD);
			startActivity(intent11);
			break;
		case R.id.click_device_deng:
			// Intent intent13 = new
			// Intent(getApplicationContext(),RemoteRGBLightActivity.class);
			Intent intent13 = new Intent(getApplicationContext(), BrandSelectionActivity.class);
			intent13.putExtra("type", DeviceType.DEVICE_TYPE_DENG);
			startActivity(intent13);
			break;

		default:
			break;
		}
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_remote_selection;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		characterParser = CharacterParser.getInstance();
		pinyinComparator = new PinyinComparator();
		mBackL = $(R.id.back_layout);
		mStatusBar = $(R.id.tv_statusBar_mian);
		mTitleTv = $(R.id.title_tv);
		mDiyL = $(R.id.click_device_diy);
		mAirL = $(R.id.click_device_kongtiao);
		mFanL = $(R.id.click_device_fengshang);
		mTvL = $(R.id.click_device_tv);
		mSetL = $(R.id.click_device_jidianhe);
		mProL = $(R.id.click_device_touying);
		mAudioL = $(R.id.click_device_yinxiang);
		mBoxL = $(R.id.click_device_box);
		mCameraL = $(R.id.click_device_cammare);
		mDvdL = $(R.id.click_device_dvd);
		mLightL = $(R.id.click_device_deng);
		remoteSelectionActivity = this;
		boolVersion();
		mBackL.setOnClickListener(this);
		mDiyL.setOnClickListener(this);
		mAirL.setOnClickListener(this);
		mFanL.setOnClickListener(this);
		mTvL.setOnClickListener(this);
		mSetL.setOnClickListener(this);
		mProL.setOnClickListener(this);
		mAudioL.setOnClickListener(this);
		mBoxL.setOnClickListener(this);
		mCameraL.setOnClickListener(this);
		mDvdL.setOnClickListener(this);
		mLightL.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		// 如果已经保存过空调品牌，则不做处理
		if (BrandManager.getInstance().isSave()) {
			return;
		} else {
			rc = new RemoteClient(getApplicationContext());
			rc.load_brands(new CallbackOnBrandLoaded() {
				
				@Override
				public void onBrandLoaded(int errcode, List<Brand> brands) {
					// TODO Auto-generated method stub
					bs = rc.getBrandByType(DeviceType.DEVICE_TYPE_AIR_CONTROLLER, "");
					Log.i("wmy", "获取品牌数 = " + brands.size());
					for (int i = 0; i < bs.size(); i++) {
						Brand b = bs.get(i);
						BrandSort bsort = new BrandSort();
						bsort.setName(b.getBrand_cn());
						if (b.getBrand_cn() != null) {
							String pinyin = characterParser.getSelling(b.getBrand_cn());
							if (pinyin.length() > 1) {
								String sortString = pinyin.substring(0, 1).toUpperCase();
								if (sortString.matches("[A-Z]")) {
									bsort.setSortLetters(sortString.toUpperCase());
								} else {
									bsort.setSortLetters("#");
								}
							} else {
								bsort.setSortLetters("#");
							}
						} else {
							bsort.setSortLetters("#");
						}
						bsort.setName_en(b.getBrand_en());
						bsort.setBrandId(b.getId());
						brandSorts.add(bsort);
					}
					Collections.sort(brandSorts, pinyinComparator);
					BrandManager.getInstance().clearBrand();
					BrandManager.getInstance().setBrandSorts(brandSorts);
					BrandManager.getInstance().setSave(true);
				}
			});
//			rc.getDataBrands(DeviceType.DEVICE_TYPE_AIR_CONTROLLER, 0, new CallbackOnBrandLoaded() {
//
//				@Override
//				public void onBrandLoaded(int arg0, List<Brand> brands) {
//					// TODO Auto-generated method stub
//					bs = brands;
//					Log.i("wmy", "获取品牌数 = " + brands.size());
//					for (int i = 0; i < bs.size(); i++) {
//						Brand b = bs.get(i);
//						BrandSort bsort = new BrandSort();
//						bsort.setName(b.getBrand_cn());
//						if (b.getBrand_cn() != null) {
//							String pinyin = characterParser.getSelling(b.getBrand_cn());
//							if (pinyin.length() > 1) {
//								String sortString = pinyin.substring(0, 1).toUpperCase();
//								if (sortString.matches("[A-Z]")) {
//									bsort.setSortLetters(sortString.toUpperCase());
//								} else {
//									bsort.setSortLetters("#");
//								}
//							} else {
//								bsort.setSortLetters("#");
//							}
//						} else {
//							bsort.setSortLetters("#");
//						}
//						bsort.setName_en(b.getBrand_en());
//						bsort.setBrandId(b.getId());
//						brandSorts.add(bsort);
//					}
//					Collections.sort(brandSorts, pinyinComparator);
//					BrandManager.getInstance().clearBrand();
//					BrandManager.getInstance().setBrandSorts(brandSorts);
//					BrandManager.getInstance().setSave(true);
//				}
//			});
		}
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			// getWindow().addFlags(
			// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			mStatusBar.setLayoutParams(layoutParams1);
			mStatusBar.setVisibility(View.VISIBLE);
		} else {
			mStatusBar.setVisibility(View.GONE);
		}
	}

}
